package com.google.googlemarket.fragement;

import android.view.View;

import com.google.googlemarket.R;
import com.google.googlemarket.adapter.HomeAdapter;
import com.google.googlemarket.base.BaseFragment;
import com.google.googlemarket.bean.HomeBean;
import com.google.googlemarket.http.Urls;
import com.google.googlemarket.utils.JsonUtil;
import com.google.googlemarket.utils.Logger;
import com.google.googlemarket.view.LoadMoreListView;
import com.google.googlemarket.view.OnloadingMoreListener;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by MrGao on 2017/9/17.
 */

public class AppFragment extends BaseFragment {

    public static final int TYPE_INIT_DATA=0;
    public static final int TYPE_INIT_LOADMORE=1;
    private static final int SWITCH_PAGE = 0;

    private LoadMoreListView mListView;
    private HomeAdapter homeAdapter;

    @Override
    public CharSequence getTitle() {
        return "应用";
    }

    @Override
    public void initView() {
        mListView = findView(R.id.list_view);
        homeAdapter = new HomeAdapter(null);
        mListView.setAdapter(homeAdapter);
    }

    @Override
    public void initListener() {

        mListView.setOnLoadingMoreListener(new OnloadingMoreListener() {
            @Override
            public void onLoadingMore() {
                Logger.i("LoadMore","加载更多");
                requestAsyncTask(TYPE_INIT_LOADMORE);
            }

            @Override
            public void onClickRetry() {
                Logger.i("LoadMore","点击重试");
                requestAsyncTask(TYPE_INIT_LOADMORE);
            }
        });
    }

    @Override
    public void initData() {
        requestAsyncTask(TYPE_INIT_DATA);
    }

    @Override
    public int getContentViewLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public View getContentView() {
        return null;
    }

    @Override
    protected Object doInBackground(int type) {
        HashMap<String, String> params = new HashMap<>();
        params.put("index",homeAdapter.getCount()+"");
        String json = NetUtils.getJson(Urls.APP,params);
        return json;
    }

    @Override
    protected void onPostExecute(int requestType,Object result) {
        //这里应该加入判断，防止服务器响应返回数据异常为空导致程序崩溃，预设添加一个数据异常提示
        Logger.i(this,"requestType:"+requestType+",result:"+result.toString());
        Type type = new TypeToken<ArrayList<HomeBean.AppInfo>>(){}.getType();

        ArrayList<HomeBean.AppInfo> datas = JsonUtil.json2Bean((String) result, type);

        switch (requestType){
            case TYPE_INIT_DATA:
                if ( checkInitData(datas)){
                    homeAdapter.getDatas().addAll(datas);
                    homeAdapter.notifyDataSetChanged();
                }
                break;
            case TYPE_INIT_LOADMORE:
                mListView.onLoadMoreComplete();
                if (checkLoadMoreData(datas,mListView)){
                    homeAdapter.getDatas().addAll(datas);
                    homeAdapter.notifyDataSetChanged();
                }
                break;
        }
    }
}
